
app.controller('listEditCtrl', function($element, $scope, $state, rest, user, uploadConf, tipService, $window, $rootScope) {
	$scope.iNowData = angular.copy($scope.ngDialogData, {});
    var token = $window.localStorage.getItem("token");
    $scope.init = function(){
        //方法状态
        $scope.menuTypeList = [
            {
                "menuType" : "AVAILABLE",
                "name" : "可用"
            },
            {
                "menuType" : "FORBID",
                "name" : "禁止"
            },
            {
                "menuType" : "DELETE",
                "name" : "删除"
            }
        ];
        angular.forEach($scope.menuTypeList,function(data,index,array){
            if(data.menuType == $scope.iNowData.status){
                $scope.menuTypeVal = data;  //匹配当前点进去对应的菜单类型
            }
            
        })
        //选择状态
        $scope.isSelectedDatas = [
        	{
        		value: 1,
        		name: "已选择"
        	},
        	{
        		value: 0,
        		name: "未选择"
        	}
        ]
        angular.forEach($scope.isSelectedDatas,function(data,index,array){
            if(data.value == $scope.iNowData.isSelected){
                $scope.isSelected = data;  //匹配当前点进去对应的菜单类型
            }
        })
        //初始化所属菜单请求
        rest.decoJava({
        	type: 'post',
            url: '/tenancy-sys/admin/menu/all/list',
            data: "",
            beforeSend: function(request){
				request.setRequestHeader("token", token); 
			},
            success: function (result) {
            	$scope.upMenuDatas = [{menuName: "请选择", id: ""}];
                angular.forEach(result.result,function(item){
                	if(item.level==1){
                		$scope.upMenuDatas.push(item);
                	}
                })
                //$scope.upMenuDatas = result.result;
                //$scope.upMenuDatas.unshift({menuName: "请选择", id: ""});
                
                //弹出窗里的所属菜单列表
                angular.forEach($scope.upMenuDatas,function(data,index,array){
                    if(data.id == $scope.iNowData.menuId){
                        $scope.$apply(function(){
                            $scope.menuLookup = data; //匹配当前点进去的所属菜单
                        })
                    }
                })
            }
        })
    };
    $scope.init();
    
    //保存
    $scope.preserveSubmit = function(){
        if ($scope.menuLookup == undefined) {
            var menuLookup = "";
        }else{
            var menuLookup = $scope.menuLookup.id;
        }
        var formData = {
            "id" : $scope.iNowData.id,
            "methodName": $scope.iNowData.methodName,
            "menuId" : menuLookup,
            "status" : $scope.menuTypeVal.menuType,
            "remark" : $scope.iNowData.remark,
            "isSelected": $scope.isSelected.value,
            "methodTagName": $scope.iNowData.methodTagName
        }
        //非空校验
        var btn = true;
        if($scope.iNowData.methodName == "" || $scope.iNowData.methodName == undefined){
        	layer.tips('方法名称必填', '.methodName-Tips',{
			  tipsMore: true,
			  tips: [2,'#ed5565']
			});
    		btn =  false;
        };
        if(menuLookup == ""){
        	layer.tips('所属菜单必填', '.menuLookup-Tips',{
			  tipsMore: true,
			  tips: [2,'#ed5565']
			});
    		btn =  false;
        };
    	rest.decoJava({
    		type: 'post',
            url: '/tenancy-sys/admin/menuMethod/update',
            data: JSON.stringify(formData),
            contentType: "application/json; charset=utf-8",
            beforeSend: function(request){
				request.setRequestHeader("token", token); 
			},
        	success: function (result) {

            	layer.closeAll('loading');
                if (result) {
                    if ("0000" == result.code) {
						layer.msg(result.message, {
				        	offset:'t',
				        	icon: 1,
							time: 0, //不自动关闭
							btn: ['确定'],
							yes: function(index){
								layer.close(index);
								$scope.closeThisDialog();
						    }
						});
                    } else {
                    	layer.msg(result.message,{
		    				offset:'t'
		    			});
                    }
                }
            }
        })
    };
    
    $scope.hideBtn = function(){
    	$scope.closeThisDialog();
    }
    
});
